package com.dev.op.controller;


import com.dev.op.VO.ResultVO;
import com.dev.op.entity.User;
import com.dev.op.enums.ResultCode;
import com.dev.op.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/roles")
@PreAuthorize("hasAnyRole('admin')")
public class RoleController {

    @Autowired
    UserRoleService userRoleService;


    @GetMapping("/admin")
    public ResultVO<List<User>> getAdmins(@RequestParam("page") String page, @RequestParam("size") String size){
         return ResultVO.success(userRoleService.getAdminUsers(Integer.parseInt(page),Integer.parseInt(size)));
    }

    @GetMapping("/teacher")
    public ResultVO<List<User>> getTeachers(@RequestParam("page") String page, @RequestParam("size") String size){
        return ResultVO.success(userRoleService.getTeachers(Integer.parseInt(page),Integer.parseInt(size)));
    }


    @PutMapping("/admin/{userId}")
    public ResultVO<Boolean> grantUserAdminRole(@PathVariable("userId") String userId){
        try {
            List<String> roles = new ArrayList<>();
            roles.add("admin");
            Boolean res = userRoleService.grantUserRolesByRolesName(userId, roles);
            return ResultVO.success(res);
        }
        catch (Exception e) {
            return ResultVO.success(false);
        }
    }

    @PutMapping("/teacher/{userId}")
    public ResultVO<Boolean> grantUserTeacherRole(@PathVariable("userId") String userId){
        try {
            List<String> roles = new ArrayList<>();
            roles.add("teacher");
            Boolean res = userRoleService.grantUserRolesByRolesName(userId, roles);
            return ResultVO.success(res);
        }
        catch (Exception e) {
            return ResultVO.success(false);
        }
    }

    @DeleteMapping("/admin/{userId}")
    public ResultVO<Boolean> depriveUserAdminRole(@PathVariable("userId") String userId){
       return null;
    }


    @DeleteMapping("/teacher/{userId}")
    public ResultVO<Boolean> depriveUserTeacherRole(@PathVariable("userId") String userId){
        return null;
    }



}
